package com.attcatstudy.cas;

import com.attcatstudy.lock.CASLock;

/**
 * @author attcat
 * ---做一个有趣的程序员
 * @date 2022-11-02 20:05
 * @description
 */
public class test {
    private volatile static int sum = 0;

    static CASLock   casLock = new CASLock();

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            Thread thread = new Thread(()->{
                for(;;){
                    if(casLock.getState()==0&&casLock.cas()){
                        try {
                            for (int j = 0; j < 10000; j++) {
                                sum++;
                            }
                            System.out.println(casLock.getState());
                        }
                        finally {
                            casLock.setState(0);
                        }
                        break;
                    }


                }

            });
            thread.start();
        }

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(sum);

    }
}
